﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEngine.UI;
using UnityEngine.Video;

public class VideoOperator : MonoBehaviour {

	public GameObject goUI = null;

	public GameObject goVideoFirst = null;
	public GameObject goVideoSecond = null;


	public AudioSource bgm = null;

	public DragObj dragObj = null;

	public VideoPlayer vpFirst = null;
	public VideoPlayer vpSecond = null;

	public VideoCfgData.VideoCfg cfg = null;

	public GameObject menu = null;
	public GameObject btnback = null;
	public GameObject btnReplay = null;
	public GameObject btnGoon = null;
	public Button realbtnGoon = null;

	public AudioClip audioClip1 = null;
	public AudioClip audioClip2 = null;

	private static VideoOperator instance = null;
	public static VideoOperator GetInstance ()
	{
		return instance;
	}

	public void Init( string id )
	{
		vpFirst.prepareCompleted -= BeginPlay;

		cfg = DataManager.videoCfgData.GetCfg (id);
		if (cfg == null)
			return;

		goVideoFirst.SetActive (true);
		vpFirst.url = ResLoader.Url2LocalPath (cfg.urlVideo1);


		if (cfg.VideoType == "multi") {

			vpSecond.url = ResLoader.Url2LocalPath (cfg.urlVideo2);

			dragObj.Init (cfg.size, cfg.beginPos, cfg.endPos, OnSwitchVideo);


		}
			

		// 准备音频
		audioClip1 = null;
		audioClip2 = null;
		bgm.clip = null;

		if (string.IsNullOrEmpty (cfg.urlAudio1) && string.IsNullOrEmpty (cfg.urlAudio2))
			Begin ();
		else {
			
			if (!string.IsNullOrEmpty (cfg.urlAudio1) && cfg.urlAudio1 != "0") {
				ResLoader.Load(cfg.urlAudio1, null, (www)=>{
					audioClip1 = WWWAudioExtensions.GetAudioClip (www);

					if ( string.IsNullOrEmpty (cfg.urlAudio2) )
						Begin();
					else if ( audioClip2 != null )
						Begin();
				}, true, true);
			}
			if (!string.IsNullOrEmpty (cfg.urlAudio2) && cfg.urlAudio2 != "0") {
				ResLoader.Load(cfg.urlAudio2, null, (www)=>{
					audioClip2 = WWWAudioExtensions.GetAudioClip (www);

					if ( string.IsNullOrEmpty (cfg.urlAudio1) )
						Begin();
					else if ( audioClip1 != null )
						Begin();
				}, true, true);
			}

		}
			

	}


	void Awake()
	{
		instance = this;

	}

	// Use this for initialization
	void Start () {
		
		vpFirst.loopPointReached += OnEndVideoPlay;
		vpSecond.loopPointReached += OnEndVideoPlay;
		goUI.SetActive (false);

	}
	
	// Update is called once per frame
	void Update () {

	}

	void HideAll()
	{
		goUI.SetActive (false);
		goVideoFirst.SetActive (false);
		goVideoSecond.SetActive (false);
		menu.SetActive (false);
	}


	public void Begin()
	{
		

		goUI.SetActive (false);
		goVideoSecond.SetActive (false);
		menu.SetActive (false);


		realbtnGoon.interactable = true;


//		vpFirst.Play ();
//		playBgm (audioClip1);
//		Loading.GetInstance ().EndLoading ();

		vpFirst.prepareCompleted += BeginPlay;
		vpFirst.Prepare ();

	}

	public void BeginPlay(VideoPlayer vp)
	{
		vp.Play ();
		playBgm (audioClip1);
		Loading.GetInstance ().EndLoading ();
	}

	public void playBgm(AudioClip ac)
	{

		if ( ac == null )
			return;

		bgm.clip = ac;
		bgm.Play ();

	}

	public void OnSwitchVideo()
	{
		// switch
		Debug.Log( "onSwitchVideo...." );

		goUI.SetActive (false);
		goVideoSecond.SetActive (true);
		vpSecond.Stop ();
		vpSecond.Play ();

		playBgm (audioClip2);

//		goVideoFirst.SetActive (false);

	}

	void OnEndVideoPlay( VideoPlayer vp )
	{
		if (cfg.VideoType == "single") {
		
			EndOpenMenu ();
		} else {
		
			if (vp == vpFirst) {
			
				goUI.SetActive (true);

			} else {
			
//				vpSecond.frame = (long)vpSecond.frameCount;
//				vpSecond.Pause();
				EndOpenMenu ();
			}
		}
			


	}

	public void OnBtnBack()
	{

		vpFirst.Stop ();
		vpSecond.Stop ();
		bgm.Stop ();

//		Resources.UnloadUnusedAssets ();

		lessonUI.GetInstance ().ShowLessonInfo ();
	}

	public void OnBtnGoon()
	{
		if (nowvp != null)
			nowvp.Play ();

		if (nowas != null)
			nowas.Play ();

		menu.SetActive (false);
	}

	public void OnBtnReplay()
	{

		vpFirst.Stop ();
//		goVideoFirst.SetActive (false);
//		goVideoFirst.SetActive (true);


		playBgm (audioClip1);
		vpFirst.Play ();

		realbtnGoon.interactable = true;

		menu.SetActive (false);
	}


	VideoPlayer nowvp = null;
	AudioSource nowas = null;
	public void OnOpenMenu()
	{
		if ( menu.activeSelf )
		{
			return;
		}

		nowvp = null;
		nowas = null;

		if (vpFirst.isPlaying)
			nowvp = vpFirst;
		else if (vpSecond.isPlaying)
			nowvp = vpSecond;
		if (bgm.isPlaying)
			nowas = bgm;

		if (cfg.VideoType != "multi") {

			if ( nowvp != null )
				nowvp.Pause ();

			if (nowas != null )
				nowas.Pause ();
		}


		menu.SetActive (true);


			if (cfg.VideoType == "single") {
		
				btnback.SetActive (true);
				btnGoon.SetActive (true);
				btnReplay.SetActive (true);
			} else {
		
				btnback.SetActive (true);
				btnGoon.SetActive (false);
				btnReplay.SetActive (false);
			}

	}

	public void EndOpenMenu()
	{
		menu.SetActive (true);
		btnback.SetActive (true);
		btnGoon.SetActive (true);
		btnReplay.SetActive (true);

		realbtnGoon.interactable = false;
	}

}
